package com.wuage.interfaces.auth.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.google.common.collect.Lists;
import com.wuage.domain.model.UacPermission;
import com.wuage.infrastructure.utils.BeanUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author Shynin
 * @version 1.0
 * @date 2020-09-07 00:31
 */
@Data
@Builder
@ApiModel("菜单信息")
@NoArgsConstructor
@AllArgsConstructor
public class UacPermissionVO {
    @ApiModelProperty("菜单id")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long id;

    @ApiModelProperty("菜单名字")
    private String menuName;

    @ApiModelProperty("菜单描述")
    private String description;

    @ApiModelProperty("父级菜单Id")
    private Long parentId;

    @ApiModelProperty("权限类型")
    private String type;

    @ApiModelProperty("菜单前端路由路径")
    private String frontUrl;

    @ApiModelProperty("按钮权限标志 菜单可为空")
    private String permit;

    @ApiModelProperty("菜单图标路径")
    private String iconUrl;

    @ApiModelProperty("菜单子对象")

    private List<UacPermissionVO> children;

    public static List<UacPermissionVO> assembleMenus2VOs(List<UacPermission> uacPermissions) {
        List<UacPermissionVO> uacPermissionVOS = Lists.newArrayList();
        if(!CollectionUtils.isEmpty(uacPermissions)) {
            uacPermissions.forEach(item -> {
                UacPermissionVO uacPermissionVO = assembleMenu2VO(item);
                if(!CollectionUtils.isEmpty(item.getChildPermission())) {
                    uacPermissionVO.setChildren(assembleMenus2VOs(item.getChildPermission()));
                }
                uacPermissionVOS.add(uacPermissionVO);
            });
        }
        return uacPermissionVOS;
    }

    public static UacPermissionVO assembleMenu2VO(UacPermission uacPermission) {
        return BeanUtils.copyProperties(uacPermission, UacPermissionVO.class);
    }
}
